package com.foot.travel.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.foot.travel.R;
import com.foot.travel.event.NetRefreshEvent;
import com.foot.travel.viewholder.MyTraveItemViewholder;
import com.koalac.ironman.common.adapter.CommonAdapter;
import com.koalac.ironman.common.module.ModuleActivity;
import com.koalac.ironman.net.pojo.request.MyTravelList;
import com.koalac.ironman.net.pojo.response.MyTravelItem;
import com.koalac.ironman.viewhelper.ViewInject;
import com.koalac.ironman.widget.PullToRefreshView;
import com.koalac.ironman.widget.PullToRefreshView.OnFooterRefreshListener;
import com.koalac.ironman.widget.PullToRefreshView.OnHeaderRefreshListener;
import com.squareup.otto.Subscribe;

public class MyTravelActivity extends ModuleActivity implements
	OnHeaderRefreshListener, OnFooterRefreshListener, OnItemClickListener {

    @ViewInject(id = R.id.school_travel_container)
    com.koalac.ironman.widget.PullToRefreshView mSchoolTravelContainer;
    @ViewInject(id = R.id.school_travel_listview)
    ListView mMyTravelListView;
    @ViewInject(id = R.id.no_data_layout)
    View mNoDataLayout;
    MyTravelList mMyTravelList;
    CommonAdapter<MyTravelItem> mCommonAdapter;
    List<MyTravelItem> mMyTravelItemList;
    private static final int REQUEST_ORDER = 1;
    public static final String TRAVEL_ID = "travel_id";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setNavigationTitle("我的旅程");
	setBackButtonVisibility(true);
	init();
	mSchoolTravelContainer.setOnHeaderRefreshListener(this);
	mSchoolTravelContainer.setOnFooterRefreshListener(this);
	mMyTravelListView.setOnItemClickListener(this);
    }

    private void init() {
	mMyTravelList = new MyTravelList();
	mMyTravelItemList = new ArrayList<MyTravelItem>();
	mCommonAdapter = new CommonAdapter<MyTravelItem>(
		MyTraveItemViewholder.class, mMyTravelItemList,
		getApplicationContext(), R.layout.my_travel_item);
	mMyTravelListView.setAdapter(mCommonAdapter);
	loadData(MyTravelItem.class, LoadState.init, mMyTravelList,
		mSchoolTravelContainer, mCommonAdapter, mMyTravelItemList);
    }
    
    @Subscribe
    public void onRefresh(NetRefreshEvent netRefreshEvent) {
	setNetworkVisibility(false);
	loadData(MyTravelItem.class, LoadState.init, mMyTravelList,
		mSchoolTravelContainer, mCommonAdapter, mMyTravelItemList);
    }

    @Override
    public void onFooterRefresh(PullToRefreshView view) {
	loadData(MyTravelItem.class, LoadState.more, mMyTravelList,
		mSchoolTravelContainer, mCommonAdapter, mMyTravelItemList);
    }

    @Override
    public void onHeaderRefresh(PullToRefreshView view) {
	loadData(MyTravelItem.class, LoadState.refresh, mMyTravelList,
		mSchoolTravelContainer, mCommonAdapter, mMyTravelItemList);

    }
    @Override
	protected void showNoDataFoundToast() {
		mNoDataLayout.setVisibility(View.VISIBLE);
		((TextView)mNoDataLayout.findViewById(R.id.text)).setText("还未下过任何订单");
	}

    @Override
    protected void initLayout(ViewGroup rootContentView) {
	addContentView(R.layout.school_travel_list);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == REQUEST_ORDER) {
            String myTravelItemId =  data.getStringExtra(TRAVEL_ID);
            for (int i = 0; i < mMyTravelItemList.size(); i++) {
        	MyTravelItem travelItem = mMyTravelItemList.get(i);
        	if (travelItem.getId().equals(myTravelItemId)) {
        	    travelItem.setFlag(1);
        	    mCommonAdapter.notifyDataSetChanged();
        	    break;
		}
	    }
	}
    }
    
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
	    long id) {
	MyTravelItem myTravelItem = mMyTravelItemList.get(position);
//	if (myTravelItem.getFlag() == 1) {
//	    return;
//	}
	Intent intent = new Intent(MyTravelActivity.this,
		OrderDetailActivity.class);
	intent.putExtra(OrderDetailActivity.ORDER_ID, myTravelItem.getId());
	startActivityForResult(intent, REQUEST_ORDER);
    }

}
